package com.example.androidproject.chat;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.androidproject.R;
import com.example.androidproject.chat.pojo.ChatMessage;
import com.example.androidproject.sqlite.MessageRepository;

import java.util.List;
public class ChatActivity extends AppCompatActivity {
    private TextView textName;
    private EditText inputMessage;
    private FrameLayout layoutSend;
    private AppCompatImageView imageBack;
    private ImageView isDeleteButton;
    private boolean isDelete = false;
    private RecyclerView recyclerView;
    private ChatMessageAdapter adapter;
    private List<ChatMessage> chatMessages;
    private String groupName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_chat2); // 设置正确的布局文件

        // 初始化控件
        textName = findViewById(R.id.textName);
        inputMessage = findViewById(R.id.inputMessage);
        layoutSend = findViewById(R.id.layoutSend);
        imageBack = findViewById(R.id.imageBack);
        recyclerView = findViewById(R.id.chatRecylerView);
        isDeleteButton = findViewById(R.id.isDelete);
        isDeleteButton.setVisibility(View.VISIBLE);
        isDeleteButton.setOnClickListener(v->{
            if(isDelete){
                isDelete = false;
                isDeleteButton.setImageResource(R.drawable.message_delete);
                adapter.setDelete(isDelete);
            }else {
                isDelete = true;
                isDeleteButton.setImageResource(R.drawable.cancel);
                adapter.setDelete(isDelete);
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        // 获取传递的数据
        Intent intent = getIntent();
        groupName = intent.getStringExtra("CHAT_GROUP_INFO");
        if (groupName != null) {
            textName.setText(groupName);
            chatMessages = loadChatMessages();
            adapter = new ChatMessageAdapter(chatMessages,new MessageRepository(this,groupName));
            recyclerView.setAdapter(adapter);
            findViewById(R.id.progressBar).setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        } else {
            Log.e("ChatActivity", "ChatGroupInfo is null");
        }

        // 设置点击事件
        imageBack.setOnClickListener(v -> onBackClicked());
        layoutSend.setOnClickListener(v -> onSendClicked());

    }

    private List<ChatMessage> loadChatMessages() {
        // 示例数据：根据 chatGroupId 从数据库或服务器获取实际数据
        MessageRepository messageRepository = new MessageRepository(this, groupName);
        List<ChatMessage> messages = messageRepository.getAllMessages();
        return messages;
    }

    private void onBackClicked() {
        finish(); // 关闭当前 Activity
    }

    private void onSendClicked() {
        String message = inputMessage.getText().toString();
        if (!message.isEmpty()) {
            Toast.makeText(this, "发送消息: " + message, Toast.LENGTH_SHORT).show();
            inputMessage.setText(""); // 清空输入框
        }
    }
}